/*

  Astrometry

  Copyright © 2010 - 2012 F.Hroch (hroch@physics.muni.cz)

  This file is part of Munipack.

  Munipack is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  Munipack is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with Munipack.  If not, see <http://www.gnu.org/licenses/>.

  1. create list to match:
     * projection from catalogue
     * photometry of ref
  2. for every image:
     * photometry
     * match
     * astrometry


   TODO:

     * print default parameters (in help ?)

*/

#include "munipack.h"
#include <wx/wx.h>
#include <wx/regex.h>
#include <wx/sstream.h>
#include <wx/txtstrm.h>
#include <wx/filename.h>


static void Parameters(MuniProcess *action, const MuniCmdLineParser& cmd)
{
  wxString a;
  double x,y;
  long i;

  if( cmd.Found("proj",&a) )
    action << ("PROJECTION = '" + a.Upper() + "'");
  else
    action << wxString("PROJECTION = 'GNOMONIC'");
  // PROJ ** must ** precede both REF or CAT

  if( cmd.Found("method",&a) )
    action << ("METHOD = '" + a.Upper() + "'");
  else
    action << wxString("METHOD = 'ROBUST'");

  if( cmd.Found("xcen",&x) && cmd.Found("ycen",&y) ){
    a.Printf("CRPIX = %20.15f %20.15f",x,y);
    action << a;
  }

  if( cmd.Found("acen",&x) && cmd.Found("dcen",&y) ){
    a.Printf("CRVAL = %20.15f %20.15f",x,y);
    action << a;
  }

  if( cmd.Found("scale",&x) ) {
    a.Printf("SCALE = %25.15e",x);
    action << a;
  }

  if( cmd.Found("angle",&x) ){
    a.Printf("ANGLE = %20.15f",x);
    action << a;
  }

  if( cmd.Found("tol",&x) ){
    a.Printf("TOL = %f",x);
    action << a;
  }

  if( cmd.Found("units",&a) && 
      ( a == "arcsec" || a == "arcmin" || a == "deg") )
    action << ("AUNITS = '" + a.Upper() + "'");

  if( cmd.Found("acen",&x) ||  cmd.Found("dcen",&x) || 
      cmd.Found("scale",&x) || cmd.Found("angle",&x) )
    action << wxString("INITPAR = F");
  
  if( cmd.Found("disable-match") )
    action << wxString("MATCHING = F" );
  
  if( cmd.Found("nmatch",&i) ){
    a.Printf("NMATCH = %ld",i);
    action << a;
  }

  if( cmd.Found("maxmatch",&i) ){
    a.Printf("MAXMATCH = %ld",i);
    action << a;
  }

  if( cmd.Found("validity",&x) ){
    a.Printf("VALIDITY = %f",x);
    action << a;
  }

  if( cmd.Found("key-alpha",&a) )
    action << ("KEY_ALPHA = '" + a + "'");

  if( cmd.Found("key-delta",&a) )
    action << ("KEY_DELTA = '" + a + "'");

  if( cmd.Found("verbose") )
    action << wxString("VERBOSE = T");

  //  if( cmd.Found("o",&a) ) {
  //    action << ("OUTPUT = '" + a + "'");
  //  }

  if( cmd.Found("t",&a) )
    action << ("REF = '" + a + "'");

  if( cmd.Found("c",&a) )
    action << ("CAT = '" + a + "'");

  if( cmd.Found("W") || cmd.Found("wcs-write") ) {

    action << wxString("WCSSAVE = T");

    const wxString suffix("~");

    if( cmd.Found("disable-backup") )

      for(size_t i = 1; i < cmd.GetParamCount(); i++) {
	wxFileName bf(cmd.GetParam(i));
	if( bf.IsFileWritable() )
	  action << ("FILE = '"+cmd.GetParam(i)+"' '" + cmd.GetParam(i) + "'");
	else
	  wxLogMessage("File `"+cmd.GetParam(i) +
		       "' has no write permissions. Skipping.");
      }

    else 

      for(size_t i = 1; i < cmd.GetParamCount(); i++) {
	wxString b(cmd.GetParam(i)+suffix);
	wxFileName bf(b);
	if( bf.FileExists() )
	  wxLogMessage("Backup file for `"+cmd.GetParam(i) +
		       "' already exists or has no write permissions. Skipping.");
	else
	  action << ("FILE = '" + cmd.GetParam(i) + "' '" + b + "'");
      }
  }
  else

    for(size_t i = 1; i < cmd.GetParamCount(); i++)
      action << ("FILE = '"+cmd.GetParam(i)+"' ''");

  
}

bool Munipack::astrometry(MuniCmdLineParser& cmd)
{
  bool write = cmd.Found("W") || cmd.Found("wcs-write");

  if( write && ! cmd.Found("c") && ! cmd.Found("t") ) {
    // both catalogue and table are unspecified

    MuniProcess *action = new MuniProcess(&pipe,"wcswrite");
    pipe.push(action);

    Parameters(action,cmd);

  }
  else {

    MuniProcess *action = new MuniProcess(&pipe,"astrometry");
    pipe.push(action);

    Parameters(action,cmd);
    
  }

  pipe.Start();

  return true;

  // catalogue or image ?
  // if catalogue
  //     ra,dec -> vo (UCAC)
  // photometry
  // astrometry



  // catalogue or image ?
  // if catalogue
  //    ra,dec -> vo (UCAC) -> projection -> table
  // if image
  //    list table

  // for every image
  //    aperture photometry
  //    match to reference table
  //    if catalogue
  //       astrometry


}
